/*
 * JGrafoCompleto.java
 *
 * Created on 3 de Junho de 2007, 13:42
 */

package dharsudoku.gui.testes;

import dharsudoku.core.grafo.Grafo;
import dharsudoku.core.grafo.GrafoCompleto;
import dharsudoku.core.vertice.Vertice;
import dharsudoku.core.vertice.VerticeCoord;

/**
 *
 * @author  rodrigo
 */
public class JGrafoCompleto extends javax.swing.JFrame {
    
    /** Creates new form JGrafoCompleto */
    public JGrafoCompleto() {
	initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        canvasGrafoCompleto = new CanvasGrafo();
        sliderM = new javax.swing.JSlider();
        labelM = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Grafo Completo");
        setName("frameGrafoCompleto");
        setResizable(false);
        canvasGrafoCompleto.setBackground(new java.awt.Color(255, 255, 255));

        sliderM.setMaximum(50);
        sliderM.setValue(0);
        sliderM.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                sliderMStateChanged(evt);
            }
        });

        labelM.setText("0");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(sliderM, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 511, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(labelM, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 52, Short.MAX_VALUE))
                    .add(canvasGrafoCompleto, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(canvasGrafoCompleto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 439, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(sliderM, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(labelM))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-609)/2, (screenSize.height-528)/2, 609, 528);
    }// </editor-fold>//GEN-END:initComponents

    private void sliderMStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sliderMStateChanged
	labelM.setText(Integer.toString(sliderM.getValue()));
	
	Grafo grafo = new Grafo();
	
	double t = sliderM.getValue();
	
	/* Itera grafo completo */
	for (int c = 1; c <= sliderM.getValue(); c++) {
	 
	    grafo.adicionar(new VerticeCoord(c, 250 + (int)(Math.sin(((double)c/t)*(Math.PI*2.0f)) * 150.0f),
						  250 + (int)(Math.cos(((double)c/t)*(Math.PI*2.0f)) * 150.0f)
						  ));
	}	
    
    /* Efetua a ligação de todos os vértices */
	for (java.util.Iterator i = grafo.getVertices().iterator(); i.hasNext(); ) {
	    Vertice A = (Vertice)i.next();
	    
	    for (java.util.Iterator j = grafo.getVertices().iterator(); j.hasNext(); ) {
		Vertice B = (Vertice)j.next();
		
		if (A != B && !A.isAdjacente(B)) {
		    try {
			A.ligar(B);
		    } catch (Exception ex) {
			continue;
		    }
		}
	    }
	}
	
	((CanvasGrafo)canvasGrafoCompleto).setGrafo(grafo);
	canvasGrafoCompleto.repaint();
    }//GEN-LAST:event_sliderMStateChanged
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {
		new JGrafoCompleto().setVisible(true);
	    }
	});
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Canvas canvasGrafoCompleto;
    private javax.swing.JLabel labelM;
    private javax.swing.JSlider sliderM;
    // End of variables declaration//GEN-END:variables
    
}
